################################################################################
#
#			Makefile for SpEden 
#
#	speden.c is a little driver program, directing control to one of 
#		the branches of SpEden:
#
#	apodfc.c does a Wilson-like calculation to apodize fcalc files;       
#	apodfo.c does a Wilson-like calculation to apodize fobs files;       
#	back.c transforms an hkl file onto a grid in physical space;
#	convert.c reformats between .bin and View or .map format;
#	distance.c measures a metric distance among multiple Np-space files;
#	maketar.c creates targets and weights from a model; 
#       perturbhkl.c perturbs an fcalc file;
#	regrid.c converts el/voxel data to sampled densities (postprocessor);
#	solve.c is the main solver module with or w/o Mir, MAD;
#	variance.c measures variance among el/voxelfiles. 
#
#	Other lower-level files:
#
#	apodutil.c contains functions used by both Apodfc and Apodfo;
#	basics.c handles input parameters common to all Eden programs;
#	ccg.c contains the least squares conjugate gradient solver;
#	ccgutil.c provides the interface between Back/Solve and ccg;
#	cost.c oversees the cost function calculations for Solve and Back;  
#	concost.c does the constraint cost function calculations;
#	crysutil.c contains crystallographic computational functions;
#	fftshare.c has functions that are shared among codes that call FFT's;
#	hdfutil.c handles I/O in HDF format;
#	hklread.c handles read functions in (hkl) space;
#	hklwrite.c handles write funciotns in (hkl) space;
#	hklutil.c handles write functions & utilities in (hkl) space;
#	highres.c handles high-resolution processing for Solve.
#	init.c handles input and initialization functions for Solve and Back;
#       qshare.c handles Np-space functions;
#	util.c handles general utilities needed by all programs;
#	
################################################################################
#######################################################################
# 	Directory where the NFFT include and lib are
#   Obtain NFFT2 from here:  http://www-user.tu-chemnitz.de/~potts/nfft/download/nfft2.0.3.tar.gz
#######################################################################
FFT		    = /path/to/nfft2
LIB  		= $(FFT)/lib
INC  		= $(FFT)/include
### FFTW		= /sw
FFTW		= /path/to/fftw
WLIB		= $(FFTW)/lib
WINC		= $(FFTW)/include
#HINC		= /Users/szoke/Programs/HDF/include
#HLIB		= /Users/szoke/Programs/HDF/lib

#######################################################################
# 	Directory to install the binary version
#######################################################################
BIN 		= /installpath/bin
VERSION		= V2.1

#######################################################################
# 	Compiler flags for optimization and/or debugging
#
# -O2 -ieee 	for no core dump
# -O2  -g 		for debug
# -O3 on HP, -O2 on SGI or DEC.
#
#######################################################################
CC		=gcc
CFLAGS		=-Wall -g 
#-I$(HINC)
ifneq ($(FFT),/usr/local) 
CFLAGS		+= -I$(INC)
endif
ifneq ($(FFTW),/usr/local)
CFLAGS		+= -I$(WINC)
endif

#######################################################################
#			NOTE:
# DOUBLESWITCH = ON	for double precision;
# DOUBLESWITCH = OFF	for single precision.
#
# Use different libraries and include files for single and
# double precision versions of Speden.
#######################################################################
LDFLAGS	 = -L$(LIB) -L$(WLIB) -lhdf5 -lnfft -lm -lfftw3 -g -pg
#-L$(HLIB)
DOUBLESWITCH 	= OFF

ifeq ($(DOUBLESWITCH),ON)
LDFLAGS += -ldfftw 
CFLAGS	+= -DDOUBLE
TARGET	 = dspeden
else
LDFLAGS += -lsfftw 
TARGET	 = sspeden
endif

.c.o:
	$(CC) $(CFLAGS) -c $<

OBJ	= 	apodfc.o   apodfo.o     back.o     convert.o  distance.o \
		maketar.o  regrid.o   solve.o      \
		apodutil.o basics.o   ccg.o        ccgutil.o  concosts.o cost.o  \
		crysutil.o fftshare.o hdfutil.o  highres.o hklread.o  hklutil.o   \
		hklwrite.o init.o     perturbhkl.o qshare.o    \
		util.o     variance.o   maputil.o  \
		weedfc.o   weedfo.o

all:		speden	

speden:  	speden.o   $(OBJ)
		$(CC)  $^ $(LDFLAGS) -o $@

install:	all
		mv speden $(BIN)/$(TARGET)
		chmod 751 $(BIN)/$(TARGET)
		chmod +x mkspeden.csh
		./mkspeden.csh $(BIN) $(BIN)/$(TARGET)

dist:		clean
		cd ../..; tar -cvf speden_$(VERSION).tar --exclude CVS --exclude \#\*\# --exclude \*~ --exclude Misc --exclude .\* speden; gzip -f speden_$(VERSION).tar 
clean:
		rm -f speden speden.o $(OBJ) 

#######################################################################
# 	Dependencies for all the files, entered manually
#	Note: speden.h includes util.h, cellparams.h, symmetry.h, dims.h
######################################################################
speden.h:	util.h cellparams.h symmetry.h dims.h
speden.o:  	speden.c util.h 
apodfc.o:	apodfc.c cellparams.h dims.h util.h apodize.h sp.h
apodfo.o:	apodfo.c cellparams.h dims.h sp.h util.h apodize.h 
apodutil.o:	apodutil.c cellparams.h dims.h util.h apodize.h
back.o:		back.c cost.h speden.h speden_Np.h ccg.h sp.h
basics.o:	basics.c cellparams.h dims.h symmetry.h util.h 
ccg.o:		ccg.c ccg.h util.h 
ccgutil.o:	ccgutil.c ccg.h dims.h speden_Np.h util.h
concosts.o:	concosts.c speden.h speden_Np.h cost.h cost.h
convert.o:	convert.c util.h cellparams.h dims.h 
cost.o:		cost.c speden.h speden_Np.h cost.h sp.h
crysutil.o:	crysutil.c util.h cellparams.h dims.h symmetry.h sp.h 
distance.o:	distance.c dims.h util.h 
fftshare.o:	fftshare.c dims.h util.h 
hdfutil.o:	hdfutil.c speden.h sp.h
highres.o:	highres.c  cellparams.h dims.h symmetry.h util.h
hklread.o:	hklread.c dims.h sp.h symmetry.h util.h 
hklutil.o:	hklutil.c speden.h sp.h 
hklwrite.o:	hklwrite.c dims.h sp.h symmetry.h util.h 
init.o:		init.c speden.h ccg.h
maketar.o:	maketar.c cellparams.h dims.h util.h 
maputil.o:	maputil.c  util.h cellparams.h dims.h symmetry.h
perturbhkl.o:	perturbhkl.c cellparams.h dims.h symmetry.h util.h
qshare.o:	qshare.c speden.h speden_Np.h 
regrid.o:	regrid.c cellparams.h dims.h symmetry.h util.h
solve.o:	solve.c speden.h speden_Np.h cost.h sp.h
util.o:		util.c util.h
variance.o:     variance.c dims.h util.h
weedfc.o:     	weedfc.c dims.h sp.h util.h
weedfo.o:     	weedfo.c dims.h sp.h util.h
